package com.nt.train.processor;
import com.nt.train.beanlife.MyBeanLifeService;
import com.nt.train.entity.Book;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
/**
 * 增强处理
 * @author hlf
 * @version 1.0
 * @date 2022/8/16 14:17
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Book) {
            System.out.println("MyBeanPostProcessor 前置方法 postProcessBeforeInitialization 执行了。。。");
        }
        if (bean instanceof MyBeanLifeService){
            MyBeanLifeService myBeanLifeService = (MyBeanLifeService) bean;
            myBeanLifeService.setBeanName("myBeanLifeService_008");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        //System.out.println("调用postProcessAfterInitialization（） 再次获得"+beanName+"加工机会");
        if (bean instanceof Book) {
            System.out.println("MyBeanPostProcessor 后置方法 postProcessAfterInitialization 执行了。。。");
        }
        return bean;
    }
}